package com.hl.toolkit.server.tenpay.other.demo;

import com.qq.qpay.qpay_mch_sp.*;
import com.util.LoadProperties;
import com.util.StringUtil;
import com.util.md5Util;

import java.io.File;
import java.util.Map;

public class QQpayScann {
	public static final String DEMO_MCH_ID = "1354058001";
	public static final String DEMO_SUB_MCH_ID = "1424913501";
	
	  public static final String key = "MAICOOLDIANMIANPAYmaicooldmpay88";
	
 //	public static final String key = "8934e7d15453e97507ef794cf7b0519d";

	

	public static final String  DEMO_OUT_TRADE_NO = "QQTran1497759003365452546111"; 
	public static final String CERT_FILE = "D:/qqMoney/1354058001.pfx";
	public static final String CA_FILE = "D:/qqMoney/1354058001.cer";

	public void qpay_close_order() throws Exception {
		CQpayCloseOrder obj = new CQpayCloseOrder(new DemoQpayMchKeyFinder());
		obj.set("mch_id", DEMO_MCH_ID);
		obj.set("nonce_str", "b49b24740506ac9b5e36aadccd8237fe");
		obj.set("out_trade_no", DEMO_OUT_TRADE_NO);
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);

		obj.setMch_id(DEMO_MCH_ID);
		System.out.println("=========================qpay_close_order=======");
		System.out.println(obj.getPostXml());
		System.out.println(obj.call());
		System.out.println(obj.checkSign(obj.parseResponse()));
	}
	 
	
	
	public void qpay_micro_pay() throws Exception {
		CQpayMicroPay obj = new CQpayMicroPay(new DemoQpayMchKeyFinder());

		obj.set("mch_id", DEMO_MCH_ID);
		obj.set("nonce_str", "b49b24740506ac9b5e36aadccd8237fe");
		obj.set("out_trade_no", DEMO_OUT_TRADE_NO+"A");
		
		
		obj.set("sub_mch_id", "1424913501");
		obj.set("body", "body_test_中文");
		obj.set("device_info", "WP00000001");
		obj.set("fee_type", "CNY");
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.set("notify_url", LoadProperties.loadPropertiesValue("sslApplicationURL")+"/tycoinPayApp/interface/QQPay/notiyReturn");
		obj.set("spbill_create_ip", "127.0.0.1");
		obj.set("total_fee", "1");
		obj.set("trade_type", "MICROPAY");
		obj.set("auth_code","910748599190708272");
		obj.setMch_id(DEMO_MCH_ID);
		System.out.println("=========================qpay_unified_order=======");
		System.out.println(obj.getPostXml());
 		String result=obj.call();
		System.out.println("返回的结果为："+result);
		System.out.println(obj.checkSign(obj.parseResponse()));
   	}

	public Map<String,String> qpayMicroPay(String total_fee,String auth_code,String out_trade_no) throws Exception {
		CQpayMicroPay obj = new CQpayMicroPay(new DemoQpayMchKeyFinder());

		obj.set("mch_id", DEMO_MCH_ID);
		obj.set("nonce_str", "b49b24740506ac9b5e36aadccd8237fe");
		obj.set("out_trade_no", out_trade_no);
 		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.set("body", "body_test_中文");
		obj.set("device_info", "WP00000001");
		obj.set("fee_type", "CNY");
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.set("notify_url", LoadProperties.loadPropertiesValue("sslApplicationURL")+"/tycoinPayApp/interface/QQPay/notiyReturn");
		obj.set("spbill_create_ip", "127.0.0.1");
		obj.set("total_fee", total_fee);
		obj.set("trade_type", "MICROPAY");
		obj.set("auth_code",auth_code);
		obj.setMch_id(DEMO_MCH_ID);
		System.out
				.println("=========================qpay_unified_order=======");
		System.out.println(obj.call());
		System.out.println(obj.checkSign(obj.parseResponse()));
		Map<String,String> resultmap=obj.parseResponse(); 
		return resultmap;
 	}

	public void qpay_unified_order() throws Exception {
		CQpayUnifiedOrder obj = new CQpayUnifiedOrder(
				new DemoQpayMchKeyFinder());

		obj.set("mch_id", DEMO_MCH_ID);
		obj.set("nonce_str", "b49b24740506ac9b5e36aadccd8237fe");
		obj.set("out_trade_no", DEMO_OUT_TRADE_NO);
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.set("body", "body_test_中文");
		obj.set("device_info", "WP00000001");
		obj.set("fee_type", "CNY");
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.set("notify_url", LoadProperties.loadPropertiesValue("sslApplicationURL")+"/tycoinPayApp/interface/QQPay/notiyReturn");
		obj.set("spbill_create_ip", "127.0.0.1");
		obj.set("total_fee", "1");
		obj.set("trade_type", "NATIVE");

		obj.setMch_id(DEMO_MCH_ID);
		System.out
				.println("=========================qpay_unified_order=======");
		System.out.println(obj.getPostXml());
		System.out.println(obj.call());
		System.out.println(obj.checkSign(obj.parseResponse()));
	}
	
	
	
	public Map<String,String> qpayUnifiedOrder(String total_fee,String goodsName,String out_trade_no) throws Exception {
		CQpayUnifiedOrder obj = new CQpayUnifiedOrder(
				new DemoQpayMchKeyFinder());

		obj.set("mch_id", DEMO_MCH_ID);
		obj.set("nonce_str", "b49b24740506ac9b5e36aadccd8237fe");
		obj.set("out_trade_no", out_trade_no); 
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.set("body", goodsName);
		obj.set("device_info", "WP00000001");
		obj.set("fee_type", "CNY");
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.set("notify_url", LoadProperties.loadPropertiesValue("applicationURL")+"/tycoinPayApp/interface/QQPay/notiyReturn");
		obj.set("spbill_create_ip", "127.0.0.1");
		obj.set("total_fee", total_fee);
		obj.set("trade_type", "NATIVE");

		obj.setMch_id(DEMO_MCH_ID);
		System.out
				.println("=========================qpay_unified_order=======");
		System.out.println(obj.getPostXml());
		String result=obj.call();
		System.out.println("返回的结果为："+result);
		Map<String,String> resultmap=obj.parseResponse();
 		return resultmap;
	}

	public void qpay_order_query() throws Exception {
		CQpayOrderQuery obj = new CQpayOrderQuery(new DemoQpayMchKeyFinder());

		obj.set("mch_id", DEMO_MCH_ID);
		obj.set("nonce_str", "b49b24740506ac9b5e36aadccd8237fe");
		obj.set("out_trade_no", DEMO_OUT_TRADE_NO);
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.setMch_id(DEMO_MCH_ID);
		System.out.println("=========================qpay_order_query=======");
		System.out.println(obj.getPostXml());
		System.out.println(obj.call());
		System.out.println(obj.checkSign(obj.parseResponse()));
	}
	
	
	public Map<String,String> qpayOrderQuery(String transaction_id,String out_trade_no) throws Exception {
		CQpayOrderQuery obj = new CQpayOrderQuery(new DemoQpayMchKeyFinder());
      Map<String,String> resultmap;
		obj.set("mch_id", DEMO_MCH_ID);
		obj.set("nonce_str", "b49b24740506ac9b5e36aadccd8237fe");
		if(StringUtil.isNotNull(transaction_id)){
			obj.set("transaction_id", transaction_id);
		}
		if(StringUtil.isNotNull(out_trade_no)){
 		obj.set("out_trade_no", out_trade_no);
		}
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.setMch_id(DEMO_MCH_ID);
		System.out.println("=========================qpay_order_query=======");
		System.out.println(obj.getPostXml());
 		System.out.println(obj.call());
		System.out.println(obj.checkSign(obj.parseResponse()));
		resultmap=obj.parseResponse();
		return resultmap; 
	}

	public void qpay_refund() throws Exception {
		CQpayRefund obj = new CQpayRefund(new DemoQpayMchKeyFinder());
		obj.set("mch_id", "1900000109");
		obj.set("nonce_str", "b49b24740506ac9b5e36aadccd8237fe");
		obj.set("out_trade_no", "20160512161914_BBC");
		obj.set("sub_mch_id","1900005911");
		obj.set("out_refund_no", "20160512161914_BBC_out_refund_1");
		obj.set("refund_fee", "1");
		obj.set("op_user_id", "1900005911");
		obj.set("op_user_passwd", "1900005911");
		
		/*
		 * 设置证书
		 */
		obj.setCertInfo(new File("D:/qqMoney/1900000109_532398_new.pfx"), "532398");
		obj.setCaInfo(new File("D:/qqMoney/api_qpay_qq_com_ca.crt"));

		System.out.println("=========================qpay_refund=======");
		System.out.println(obj.getPostXml());
		String result=obj.call();
		System.out.println(result);
		System.out.println(obj.checkSign(obj.parseResponse()));
	}
	
	public Map<String,String> qpayRefund(String out_trade_no,String refund_fee) throws Exception {
		CQpayRefund obj = new CQpayRefund(new DemoQpayMchKeyFinder());
	      Map<String,String> resultmap;
		
		obj.set("mch_id", DEMO_MCH_ID);
		obj.set("nonce_str", "b49b24740506ac9b5e36aadccd8237fe");
		obj.set("out_trade_no", out_trade_no);
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.set("out_refund_no", out_trade_no+(long) (Math.random() * 1000));
		obj.set("refund_fee",refund_fee);
		obj.set("op_user_id", DEMO_MCH_ID);
		obj.set("op_user_passwd", md5Util.ENcode32(LoadProperties.loadPropertiesValue("qq_opUserPasswd")));
		
		/*
		 * 设置证书
		 */
		obj.setCertInfo(new File(CERT_FILE), "1354058001");
		obj.setCaInfo(new File(CA_FILE)); 
		System.out.println("=========================qpay_refund=======");
		System.out.println(obj.getPostXml());
		String resultStr=obj.call();
		System.out.println(resultStr);
		System.out.println(obj.checkSign(obj.parseResponse()));
		resultmap=obj.parseResponse();
		return resultmap;
	}


	public void qpay_reverse() throws Exception {
		CQpayReverse obj = new CQpayReverse(new DemoQpayMchKeyFinder());
		obj.set("mch_id", DEMO_MCH_ID);
		obj.set("nonce_str", "b49b24740506ac9b5e36aadccd8237fe");
		obj.set("out_trade_no", DEMO_OUT_TRADE_NO);
		obj.set("sub_mch_id", DEMO_SUB_MCH_ID);
		obj.set("op_user_id", DEMO_SUB_MCH_ID);
		obj.set("op_user_passwd", "");
		

		/*
		 * 设置证书
		 */
		obj.setCertInfo(new File(CERT_FILE), "1354058001");
		obj.setCaInfo(new File(CA_FILE));

		System.out.println("=========================qpay_reverse=======");
		System.out.println(obj.getPostXml());
		System.out.println(obj.call());
		System.out.println(obj.checkSign(obj.parseResponse()));
	}

	public static void main(String[] args) throws Exception {
		QQpayScann qQpayScann = new QQpayScann();
		//		qQpayScann.qpay_refund();
		// demo.qpay_unified_order();
//		demo.qpay_order_query();
//		demo.qpay_close_order();
//		demo.qpay_refund();
//		demo.qpay_reverse();
		qQpayScann.qpay_order_query();
	}
}
